package com.huakang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huakang.entity.Medicion;
import com.huakang.entity.Search;
import com.huakang.service.MedicionService;
import com.huakang.utils.ResultVo;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/medicion")
public class MedicionController {

    @Autowired
    private MedicionService medicionService;

    @GetMapping("/findAll")
    public ResultVo medicionList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Medicion> medicionList = medicionService.findAll();
        PageInfo<Medicion> pageInfo = new PageInfo<>(medicionList);
        return ResultVo.success("显示成功", pageInfo);
    }

    @PostMapping("/save")
    public ResultVo save(@RequestBody Medicion medicion) {
        boolean flag = medicionService.save(medicion);
        if (flag) {
            return ResultVo.success("新增成功");
        }
        return ResultVo.fail("新增失败");
    }

    @PutMapping("update")
    public ResultVo update(@RequestBody Medicion medicion) {
        boolean flag = medicionService.update(medicion);
        if (flag) {
            return ResultVo.success("修改成功");
        }
        return ResultVo.fail("修改失败");
    }

    @PutMapping("updatenum")
    public ResultVo updatenum(@RequestBody Medicion medicion) {
        boolean flag = medicionService.updatenum(medicion);
        if (flag) {
            return ResultVo.success("更新成功");
        }
        return ResultVo.fail("更新失败");
    }

    @PostMapping("/medSearch")
    public ResultVo medSearch(@RequestBody Search search, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Medicion> medicionList = medicionService.medSearch(search);
        PageInfo<Medicion> pageInfo = new PageInfo<>(medicionList);
        return ResultVo.success("查询完成", pageInfo);
    }
    @RequestMapping("/findAll1")
    public ResultVo findAll1(){
        List<Medicion> medicionList = medicionService.findAll();
        return ResultVo.success("查询成功",medicionList);
    }
    @GetMapping("/updateStock")
    public ResultVo updateStock(@Param("medid") Integer medid, @Param("mednum") Integer mednum){
        System.out.println("med==="+medid);
        System.out.println("mednum==="+mednum);
        return  medicionService.updateStock(medid, mednum);
    }
    @GetMapping("/findMedid")
    public ResultVo findMedid(Integer medid){
        return ResultVo.success("成功",medicionService.findMedid(medid));
    }
}
